package action.ueditor;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import staticclass.CommonConfig;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

import common.impl.exception.ServiceException;
import common.interf.IErrorHandle;
import common.interf.IFileHandle;
import common.interf.IImage;
import common.interf.IServletHandle;

public class UEditorAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private String filePath="/upload/images";
	private IImage iImage;
	private IServletHandle servletHandle;
	private IFileHandle fileHandle;
	private IErrorHandle errorHandle;
	private File file;
	private String fileContentType;
	private String fileFileName;
	private Integer size=0;
	private Integer width=0;
	private Integer height=0;
	private String action;
	private final String uploadUrl = "/weblab/ueditor/upload.action";
	private final String uploadPath = "/upload/ueditor/images";
	private final String fieldName = "file";
	private final Integer imageMaxSize = 2048;
	
//	"imageActionName": "uploadimage", /* 执行上传图片的action名称 */
//    "imageFieldName": "upfile", /* 提交的图片表单名称 */
//    "imageMaxSize": 2048000, /* 上传大小限制，单位B */
//    "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
//    "imageCompressEnable": true, /* 是否压缩图片,默认是true */
//    "imageCompressBorder": 1600, /* 图片压缩最长边限制 */
//    "imageInsertAlign": "none", /* 插入的图片浮动方式 */
//    "imageUrlPrefix": "", /* 图片访问路径前缀 */
//    "imagePathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
	private String imageAllowFiles[] = {".png", ".jpg", ".jpeg", ".gif", ".bmp"};
	
	

	public String getAction() {
		return action;
	}


	public void setAction(String action) {
		this.action = action;
	}


	public void setServletHandle(IServletHandle servletHandle) {
		this.servletHandle = servletHandle;
	}
	
	
	
	public File getFile() {
		return file;
	}


	public void setFile(File file) {
		this.file = file;
	}


	public String getFileContentType() {
		return fileContentType;
	}


	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}


	public String getFileFileName() {
		return fileFileName;
	}


	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}


	public void setFileHandle(IFileHandle fileHandle) {
		this.fileHandle = fileHandle;
	}

	

	public void setiImage(IImage iImage) {
		this.iImage = iImage;
	}


	public String execute(){
		return action;
	}
	
	/**
	 * 返回参数配置
	 * @return
	 */
	public void config(){
		JSONObject jret = new JSONObject();
		jret.put("imageUrl",uploadUrl);
		jret.put("imagePath",uploadPath);
		jret.put("imageFieldName",fieldName);
		jret.put("imageMaxSize",imageMaxSize);
		jret.put("imageAllowFiles", imageAllowFiles);
		jret.put("imageActionName", "upload");
		jret.put("imageUrlPrefix", CommonConfig.domainName);
//		jret.put("imagePathFormat", uploadPath+"/{yyyy}-{mm}-{dd}_{rand:6}_{filename}");
		servletHandle.writeToClient(ServletActionContext.getResponse(), jret.toString());
	}
	
	public void upload(){
		String s="";
		try{
			HttpServletRequest h = ServletActionContext.getRequest();
			if(!iImage.checkImage(file, size, width, height)){
				throw new ServiceException("ERR-0020");
			}
			String serverUrl = servletHandle.getServerPath(ServletActionContext.getRequest());
			s = fileHandle.saveUploadFile(file, ServletActionContext.getServletContext().getRealPath("/") + uploadPath, fileFileName);
			s = CommonConfig.contextPath + uploadPath + "/" + s;
			
			JSONObject jo = new JSONObject();
			jo.put("url", s);
			jo.put("type", ".jpg");
			jo.put("state", "SUCCESS");
			s = jo.toString();
		}catch(RuntimeException e){
			s = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), s);
	}
	
}
